package com.ruoyi.web.controller.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 生成验证码
     */
    @RequestMapping("/login")
    public AjaxResult getCode() {
        AjaxResult ajax = AjaxResult.success();
        boolean captchaOnOff = loginService.captchaOnOff();
        ajax.put("captchaOnOff", captchaOnOff);
        if (!captchaOnOff) {
            return ajax;
        }

        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String captcha = loginService.getCaptchaImg(uuid);
        ajax.put("uuid", uuid);
        ajax.put("img", captcha);
        return ajax;
    }
}
